<?php

namespace app\controller;

use app\common\controller\BaseController;
use app\common\service\JwtService;
use app\common\service\UploadService;
use app\common\utils\ErrorCode;
use app\common\utils\Massage;
use app\common\utils\Res;

class Upload extends BaseController
{

    public function uploads()
    {
        //token检验
        $headers = getallheaders();
        if(!isset($headers['Authorization'])){
            return Res::error('校验错误');
        }
        $token = trim(ltrim($headers['Authorization'], 'Bearer'));
        if(!$token){
            return Res::error(Massage::ROULE);
        }
        $result = JwtService::checkToken($token);
        if ($result['code'] != ErrorCode::SUCCESS) {
            return Res::jsonResult($result);
        }
        
        $files = $this->request->file('file');
        $fileType = $this->request->post('fileType');
        $setup = $this->request->post('setup');
        $by = $this->request->userName;
        $result = UploadService::uploads($files, $by, $fileType, $setup);
        if (empty($result)) {
            return Res::error('上传文件失败');
        }
        return Res::ok($result);
    }

}
